/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.cs.util.db.ioc;

import com.cs.util.db.ResultSetMetaInfo;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * GenericIOController can be extended the input and output of a database.
 * @author dimitris@jmike.gr
 */
public class GenericIOController implements IOController {

    protected final String table;
    protected final String column;
    protected final int columnIndex;
    protected boolean isNullable;
    protected long maxLength;
    protected String defaultValue;
    protected boolean locked = false;
    protected String lockValue = null;

    public GenericIOController(ResultSetMetaInfo rsmi, int columnIndex) throws SQLException {
        this.table = rsmi.getTable(columnIndex);
        this.column = rsmi.getColumn(columnIndex);
        this.columnIndex = columnIndex;
        this.isNullable = rsmi.isNullable(columnIndex);
        this.maxLength = rsmi.getSize(columnIndex);
        this.defaultValue = rsmi.getDefaultValue(columnIndex);
    }

    public GenericIOController(ResultSetMetaInfo rsmi, String table, String column) throws SQLException {
        this.table = table;
        this.column = column;
        this.columnIndex = rsmi.getPosition(column);
        this.isNullable = rsmi.isNullable(columnIndex);
        this.maxLength = rsmi.getSize(columnIndex);
        this.defaultValue = rsmi.getDefaultValue(columnIndex);
    }

    /**
     * Returns the id of this controller.
     * @return
     */
    public String getId() {
        return table + "." + column;
    }

    /**
     * Returns the label of this controller.
     * @return
     */
    public String getLabel() {
        return column;
    }

    /**
     * Returns the column name of this controller.
     * @return
     */
    public String getColumnName() {
        return column;
    }

    /**
     * Returns the table name of this controller.
     * @return
     */
    public String getTableName() {
        return table;
    }

    /**
     * Sets the default value of this controller.
     * @param defaultValue the value.
     * @throws IOCException
     */
    public void setDefaultValue(String defaultValue) throws IOCException {
        this.defaultValue = validate(defaultValue);
    }

    /**
     * Returns the default value of this controller.
     * @return
     */
    public String getDefaultValue() {
        return this.defaultValue;
    }

    /**
     * Sets an input lock for this controller.
     * When an input lock is set, the controller cannot except different values.
     * Please note that an input lock can only be set once in the lifetime of an IOController.
     * @param value the value.
     * @throws IOCException
     */
    public void setInputLock(String value) throws IOCException {
        this.lockValue = validate(value);
        this.locked = true;
    }

    public String getValue(ResultSet rs) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setParameterValue(PreparedStatement ps, int parameterIndex, String value) throws SQLException, IOCException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * Returns the value of the controller's column as a URL encoded String.
     * @param rs the resultset containing the current row position and the field's value.
     * @return
     */
    public String getURLEncodedValue(ResultSet rs) {
        final String value = this.getValue(rs);
        if (value != null) {
            try {
                return URLEncoder.encode(this.getValue(rs), "UTF-8");
            } catch (UnsupportedEncodingException ex) {//UTF-8 is not supported
            /* do nothing */
            }
        }
        return null;
    }

    public String getXhtmlTableData(ResultSet rs) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getXhtmlFormElement(ResultSet rs, boolean isEmpty) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String validate(String value) throws IOCException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String formatSQLValue(String value) throws IOCException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
